﻿using System;
using System.IO;

namespace Red.Core.IO
{
    public static class Path
    {
        public static string Normalize( string path )
        {
            return System.IO.Path.GetFullPath( new Uri( path ).LocalPath ).TrimEnd( System.IO.Path.DirectorySeparatorChar, System.IO.Path.AltDirectorySeparatorChar ).ToLowerInvariant();
        }

        public static int Compare( string path1, string path2 )
        {
            return string.Compare( Normalize( path1 ), Normalize( path2 ) );
        }

        public static bool Equal( string path1, string path2 )
        {
            return Compare( path1, path2 ) == 0;
        }

        public static bool Contains( string parent, string potential )
        {
            DirectoryInfo d1 = new DirectoryInfo( parent );
            DirectoryInfo d2 = new DirectoryInfo( potential );

            return d1.Contains( d2 );
        }

        public static bool Contains( this DirectoryInfo parent, DirectoryInfo potential )
        {
            if ( parent.FullName == potential.FullName )
            {
                return true;
            }
            else
            {
                if( potential.Parent != null )
                {
                    return parent.Contains( potential.Parent );
                }
            }

            return false;
        }
    }
}
